﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro;
using MahApps.Metro.Ext.Common;

namespace MahApps.Metro.Ext.Controls
{
    /// <summary>
    /// Interaction logic for CustomMetroWindow.xaml
    /// </summary>
    public class CustomMetroWindow : MetroWindow
    {
        protected Theme currentTheme = Theme.Light;
        protected Accent currentAccent = ThemeManager.DefaultAccents.First(x => x.Name == "Green");

        public CustomMetroWindow()
        {
            this.Initialize();
            this.Loaded += new RoutedEventHandler(CustomMetroWindow_Loaded);
        }

        protected virtual void Initialize()
        {
            this.LoadResourceDictionary();
            this.InitializeMisc();
        }

        private void LoadResourceDictionary()
        {
            var resourceDictionary = new ResourceDictionary();
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro.Ext;component/Themes/BusyIndicator.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro.Resources;component/Icons.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml") });
            resourceDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml") });
            
            this.Resources = resourceDictionary;
        }

        private void InitializeMisc()
        {
            this.TitleCaps = false;
            this.SetResourceReference(GlowBrushProperty, StyleConst.AccentColorBrushKey);
        }

        void CustomMetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ThemeManager.ChangeTheme(this, this.currentAccent, this.currentTheme);
        }

        protected virtual void ChangeTheme(Accent accent, Theme theme)
        {
            foreach (Window window in Application.Current.Windows)
                ThemeManager.ChangeTheme(window, accent, theme);
        }
    }
}